<template>
  <view class="content">
    <view v-if="JSON.stringify(list) != '[]'" class="list-block">
      <view class="item-block">
        <view class="tit">法律法规库·一键轻松查询</view>
        <view class="search-block pl-20 pr-20">
          <u-search
            placeholder="请输入法律法规关键字"
            borderColor="#eee"
            bgColor="#ffffff"
            shape="square"
            v-model="params.resName"
            action-text="搜索"
            :action-style="{ color: '#3F99F7', fontWeight: '500' }"
            @custom="submit()"
            @search="submit()"
          ></u-search>
        </view>
      </view>
      <view class="item-block mt-20" v-for="(item, index) in list" :key="index">
        <view class="header-block border-b-common pb-20">
          <view class="date-block">{{
            item.resName ? item.resName : "-"
          }}</view>
          <view class="button-warning" @click="routeToPDF(item.id)">预览</view>
        </view>
        <view class="body-block border-b-common pb-10">
          <view class="body-item pt-20 pb-25"
            >发文机构：{{ item.note ? item.note : "-" }}</view
          >
          <view class="body-item pt-20 pb-25"
            >发布时间：{{ item.createTime ? item.createTime : "-" }}</view
          >
          <view class="body-item pt-20 pb-25"
            >生效时间：{{ item.updateTime ? item.updateTime : "-" }}</view
          >
        </view>
      </view>
    </view>
    <view v-else class="empty-block">
      <u-empty text="暂无数据" mode="data" icon-size="80"></u-empty>
    </view>
  </view>
</template>

<script>
import {
  getLawListApi,
  getResourcesDataApi,
} from "@/apis/safetyProductionLaw/law.js";
export default {
  data() {
    return {
      params: {
        pageNo: 1,
        pageSize: 10,
        resType: "law_regulation",
        resName: "",
      },
      list: [],
      pushState: true,
    };
  },
  onPullDownRefresh() {
    this.getDangerEditList(
      Object.assign(this.params, { pageNo: 1, pageSize: 10 })
    );
  },
  onReachBottom() {
    if (this.pushState)
      this.getDangerEditList(
        Object.assign(this.params, { pageNo: this.params.pageNo + 1 }),
        false
      );
  },
  onShow() {
    this.getDangerEditList(Object.assign(this.params, { pageNo: 1 }));
  },
  methods: {
    getDangerEditList: function (params, freshState = true) {
      getLawListApi(params)
        .then((res) => {
          if (freshState) {
            this.pushState = true;
            this.list = res.list;
            this.list.forEach((item) => {
              if (item.createTime == null) {
                return "";
              } else {
                let date = new Date(item.createTime);
                let y = date.getFullYear(); // 年
                let MM = date.getMonth() + 1; // 月
                MM = MM < 10 ? "0" + MM : MM;
                let d = date.getDate(); // 日
                d = d < 10 ? "0" + d : d;
                let h = date.getHours(); // 时
                h = h < 10 ? "0" + h : h;
                let m = date.getMinutes(); // 分
                m = m < 10 ? "0" + m : m;
                let s = date.getSeconds(); // 秒
                s = s < 10 ? "0" + s : s;
                var timer =
                  y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
                item.createTime = timer; //转换结果为 2019-05-15 15:30:24
              }
              if (item.updateTime == null) {
                return "";
              } else {
                let date = new Date(item.updateTime);
                let y = date.getFullYear(); // 年
                let MM = date.getMonth() + 1; // 月
                MM = MM < 10 ? "0" + MM : MM;
                let d = date.getDate(); // 日
                d = d < 10 ? "0" + d : d;
                let h = date.getHours(); // 时
                h = h < 10 ? "0" + h : h;
                let m = date.getMinutes(); // 分
                m = m < 10 ? "0" + m : m;
                let s = date.getSeconds(); // 秒
                s = s < 10 ? "0" + s : s;
                var timer1 =
                  y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
                item.updateTime = timer1; //转换结果为 2019-05-15 15:30:24
              }
            });
          } else {
            this.list = this.list.concat(res.list);
          }
          if (res.list.length < this.params.pageSize) this.pushState = false;
          uni.stopPullDownRefresh();
        })
        .catch((err) => console.warn(err));
    },
    submit: function () {
      let params = JSON.parse(JSON.stringify(this.params));
      if (params.startTime)
        Object.assign(params, { startTime: `${params.startTime} 00:00:00` });
      if (params.endTime)
        Object.assign(params, { endTime: `${params.endTime} 23:59:59` });
      Object.assign(params, { pageNo: 1 });
      this.getDangerEditList(params);
    },
    routeToPDF: function (id) {
      getResourcesDataApi({ id })
        .then((res) => {
          uni.downloadFile({
            url: res.fileUrl,
            success: (res) => { 
              uni.openDocument({
                filePath: res.tempFilePath,
                showMenu: true,
                fail: () =>
                  uni.showToast({ title: "文件打开失败", icon: "none" }),
              });
            },
            fail: () =>
              uni.showToast({ title: "本地文件下载失败", icon: "none" }),
          });
        })
        .catch((err) => console.warn(err));
    },
  },
};
</script>

<style>
page {
  background-color: #f4f4f4;
}
</style>

<style lang="scss" scoped>
.border-b-common {
  border-bottom: 1rpx solid #f4f4f4;
}
.content {
  padding: 0rpx 25rpx 50rpx 25rpx;
  .tab-block {
    background-color: #ffffff;
    z-index: 99;
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
  }
  .list-block {
    .item-block {
      background-color: #ffffff;
      border-radius: 6rpx;
      box-shadow: 0 0 10rpx 0 #f4f4f4;
      padding: 25rpx;
      margin-top: 25rpx;
      .header-block {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .date-block {
          width: 460rpx;
          font-size: 35rpx;
          line-height: 35rpx;
          font-weight: 700;
          color: #333333;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .button-warning {
          @extend .button;
          font-size: 30rpx;
          color: #ff9900;
          border-color: #ff9900;
        }
      }
      .body-block {
        .body-item {
          font-size: 28rpx;
          line-height: 40rpx;
          color: #666666;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
      .footer-block {
        @extend .header-block;
        align-items: flex-end;
      }
      .button-block {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        .button {
          width: 100rpx;
        }
      }
      .tit {
        margin: 30rpx;
        font-size: 35rpx;
        font-weight: 700;
      }
    }
  }
  .empty-block {
    padding-top: 300rpx;
  }
}
</style>
